package com.zhixinglu.spring.boot.thread.threadpool;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * NamedRunnable
 * </p>
 *
 * @author weixin
 * @date 2023/3/24 14:52
 */
@Slf4j
public class NamedRunnable implements Runnable{

    private final Runnable task;
    private final String threadName;

    public NamedRunnable(Runnable task, String threadName) {
        this.task = task;
        this.threadName = threadName;
    }

    @Override
    public void run() {
        Thread.currentThread().setName(threadName);
        try {
            task.run();
        } catch (Exception e) {
            log.error("Exception in thread " + threadName, e);
        }
    }
}
